import { ref, reactive, computed, nextTick } from "vue";
import { request, catchException } from "@/util/request";

const data = {
  id: "",
  selectInvisible: true,
  topIconInvisible: true,
  tabBarInvisible: false,
  coverImgInvisible: false,
  timelineInvisible: [],
  typeBar: [
    { label: "默认", value: 1 },
    { label: "浏览量", value: 2 },
    { label: "点赞量", value: 3 },
  ],
  viewItems: [
    { label: "九宫格", value: "cell", icon: "/static/photo-wall/select1.png" },
    { label: "时间线", value: "timeline", icon: "/static/photo-wall/select2.png" },
    { label: "瀑布流", value: "waterfall", icon: "/static/photo-wall/select3.png" },
  ],
  sortItems: [
    { label: "正序", value: 1, icon: "/static/photo-wall/select4.png" },
    { label: "倒序", value: 2, icon: "/static/photo-wall/select5.png" },
  ],
  viewName: "cell",
  selectedItems: [],
  selectComposite: false,
  crtSwiperIndex: 0,
  showImgInfo: false,
  /*  */
  detail: {
    address: "",
    browse_num: NaN,
    con_end_time: "",
    con_start_time: "",
    create_time: "",
    id: NaN,
    img: "",
    intro: "",
    is_can_uploads: false,
    main_img: "",
    status: NaN,
    title: "",
    works_num: NaN,
    qr_url: "",
    time: "",
    animation: "",
    animation_time: 0,
    surplus_uploads_number: NaN,
    vote_way: 0,
  },
  listFilter: {
    page: 1,
    limit: 50,
    act_id: NaN,
    sort: 1,
    order: 2,
  },
  listData: [
    {
      browse_num: NaN,
      cover: "",
      create_time: "",
      height: NaN,
      id: NaN,
      thumb_img: "",
      vote_num: NaN,
      user_vote_num: NaN,
      width: NaN,

      head_img: "",
      nickname: "",
      ratio: "",
      size: "",
      img: "",
    },
  ],
};

const _data = JSON.parse(JSON.stringify(data));
const state = reactive(_data) as typeof data;
const crtImgInfo = computed(
  () => state.listData[state.crtSwiperIndex] || ({} as typeof state.listData[number]),
);
function scrollPageTop() {
  uni.pageScrollTo({ scrollTop: 0, duration: 100 });
}
/*  */
const refresh = ref(false);
const finished = ref(false);
const loadingMore = ref(false);
async function getList(loading = true) {
  try {
    const res = await request({
      loading,
      url: "wechat/pictureLiveWorks/list",
      data: { ...state.listFilter, token: true },
    });
    const resList = res.data?.content.data || [];
    state.detail.works_num = res.data?.content?.total;
    finished.value = resList.length < state.listFilter.limit;
    state.listData = loadingMore.value ? [...state.listData, ...resList] : [...resList];
  } catch (error) {
    if (refresh.value) state.listData = [];
    finished.value = true;
    catchException(error);
  }
  refresh.value = false;
  loadingMore.value = false;
  uni.stopPullDownRefresh();
}
function resetState() {
  const _data = JSON.parse(JSON.stringify(data));
  Object.assign(state, _data);
}

export {
  state as imgListState,
  scrollPageTop,
  getList,
  refresh,
  finished,
  loadingMore,
  crtImgInfo,
  resetState,
};
